/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.FeedSyncLibrary
{
    public class RSSFeedSyncItem : Microsoft.CSA.FeedSync.FeedSyncLibrary.FeedSyncItem
    {
        private string m_Title;
        private string m_Description;

        public RSSFeedSyncItem()
        {
        }

        public RSSFeedSyncItem(Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeed i_RSSFeed, string i_By, string i_ID, string i_Title, string i_Description) :
            this (i_RSSFeed.XmlDocument, i_RSSFeed.XmlNamespaceManager, i_By, i_ID, i_Title, i_Description)
        {
        }

        public RSSFeedSyncItem(System.Xml.XmlDocument i_XmlDocument, System.Xml.XmlNamespaceManager i_XmlNamespaceManager, string i_By, string i_ID, string i_Title, string i_Description)
        {
            if (i_XmlDocument == null)
                throw new System.ArgumentException("i_XmlDocument must be non-null!");

            if (i_XmlNamespaceManager == null)
                throw new System.ArgumentException("i_XmlNamespaceManager must be non-null!");

            if (System.String.IsNullOrEmpty(i_ID))
                throw new System.ArgumentException("i_ID must be non-null!");

            m_XmlElement = i_XmlDocument.CreateElement(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_ITEM_ELEMENT_NAME);
            m_XmlNamespaceManager = i_XmlNamespaceManager;

            m_Title = i_Title;
            m_Description = i_Description;
            
            if (!System.String.IsNullOrEmpty(m_Title))
            {
                System.Xml.XmlElement TitleXmlElement = i_XmlDocument.CreateElement(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_TITLE_ELEMENT_NAME);
                TitleXmlElement.InnerText = i_Title;
                m_XmlElement.AppendChild(TitleXmlElement);
            }

            if (!System.String.IsNullOrEmpty(m_Description))
            {
                System.Xml.XmlElement  DescriptionXmlElement = i_XmlDocument.CreateElement(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_DESCRIPTION_ELEMENT_NAME);
                DescriptionXmlElement.InnerText = i_Description;
                m_XmlElement.AppendChild(DescriptionXmlElement);
            }

            m_Sync = new Microsoft.CSA.FeedSync.FeedSyncLibrary.Sync
                (
                m_XmlElement.OwnerDocument,
                m_XmlNamespaceManager,
                i_By,
                i_ID
                );

            if (m_XmlElement.ChildNodes.Count > 0)
                m_XmlElement.InsertBefore(m_Sync.XmlElement, m_XmlElement.ChildNodes[0]);
            else
                m_XmlElement.AppendChild(m_Sync.XmlElement);

            this.Validate();
        }

        public RSSFeedSyncItem(System.Xml.XmlElement i_XmlElement, System.Xml.XmlNamespaceManager i_XmlNamespaceManager)
        {
            this.Initialize(i_XmlElement, i_XmlNamespaceManager);
        }

        public string Title
        {
            get
            {
                return m_Title;
            }
            set
            {
                System.Xml.XmlElement TitleXmlElement = (System.Xml.XmlElement)m_XmlElement.SelectSingleNode(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_TITLE_ELEMENT_NAME);
                if (TitleXmlElement != null)
                    TitleXmlElement.ParentNode.RemoveChild(TitleXmlElement);

                if (!System.String.IsNullOrEmpty(value))
                {
                    TitleXmlElement = m_XmlElement.OwnerDocument.CreateElement(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_TITLE_ELEMENT_NAME);
                    m_XmlElement.AppendChild(TitleXmlElement);
                    TitleXmlElement.InnerText = value;
                }

                m_Title = value;
            }
        }

        public string Description
        {
            get
            {
                return m_Description;
            }
            set
            {
                System.Xml.XmlElement DescriptionXmlElement = (System.Xml.XmlElement)m_XmlElement.SelectSingleNode(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_DESCRIPTION_ELEMENT_NAME);
                if (DescriptionXmlElement != null)
                    DescriptionXmlElement.ParentNode.RemoveChild(DescriptionXmlElement);

                if (!System.String.IsNullOrEmpty(value))
                {
                    DescriptionXmlElement = m_XmlElement.OwnerDocument.CreateElement(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_DESCRIPTION_ELEMENT_NAME);
                    m_XmlElement.AppendChild(DescriptionXmlElement);
                    DescriptionXmlElement.InnerText = value;
                }

                m_Description = value;
            }
        }

        public override void Update(System.DateTime? i_WhenDateTime, string i_By)
        {
            this.Validate();

            m_Sync.Update(i_WhenDateTime, i_By, true);
        }

        public override void ResolveConflicts(System.DateTime? i_WhenDateTime, string i_By)
        {
            this.Validate();

            m_Sync.ResolveConflicts(i_WhenDateTime, i_By, true);
        }

        public override void Delete(System.DateTime? i_WhenDateTime, string i_By)
        {
            this.Validate(); 

            m_Sync.Delete(i_WhenDateTime, i_By, true);
        }

        public override Microsoft.CSA.FeedSync.FeedSyncLibrary.Sync Initialize(System.Xml.XmlElement i_XmlElement, System.Xml.XmlNamespaceManager i_XmlNamespaceManager)
        {
            base.Initialize(i_XmlElement, i_XmlNamespaceManager);

            System.Xml.XmlElement m_TitleXmlElement = (System.Xml.XmlElement)m_XmlElement.SelectSingleNode(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_TITLE_ELEMENT_NAME);
            if (m_TitleXmlElement != null)
                m_Title = m_TitleXmlElement.InnerText;

            System.Xml.XmlElement m_DescriptionXmlElement = (System.Xml.XmlElement)m_XmlElement.SelectSingleNode(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_DESCRIPTION_ELEMENT_NAME);
            if (m_DescriptionXmlElement != null)
                m_Description = m_DescriptionXmlElement.InnerText;

            this.Validate();

            return m_Sync;
        }

        private void Validate()
        {
            if (System.String.IsNullOrEmpty(m_Title) && System.String.IsNullOrEmpty(m_Description))
                throw new System.ArgumentException("m_Title and m_Description cannot both be null!");

            bool InvalidElement =
                (m_XmlElement.LocalName != Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_ITEM_ELEMENT_NAME) ||
                (!System.String.IsNullOrEmpty(m_XmlElement.NamespaceURI));

            if (InvalidElement)
                throw new System.ArgumentException("Invalid m_XmlElement!");

            if (m_Sync == null)
                throw new System.ArgumentException("m_Sync must be non-null!");
        }

    }
}
